#!/usr/bin/perl

require "accounts.pl";

my $entrylocalmembers = "";
my $notlocalmembers = "";
my $umembers = "";
my $groupname = "";
my $gid = -1;
my (%group, $i, $newgid, $error_msg, @line);

&ReadParse();

$groupname = $in{"groupname"};

unless($groupname)
{
	$error_msg = $text{'error_msg_no_groupname'};
}

unless($error_msg)
{
	$error_msg = &check_simbol_uname($groupname);
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'groups_naslog_event_create_fail_gname_err_format'};
	$formatstr = sprintf $text{'groups_naslog_create_fail_gname_err_format'}, $groupname;	
}

unless($error_msg)
{
	$gid = &local_group_exist($groupname);
	if($gid >= 0)
	{
		$error_msg = $text{'error_msg_group_exist_local'};
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'groups_naslog_event_create_fail_gname_exist_local'};
		$formatstr = sprintf $text{'groups_naslog_event_create_fail_gname_exist_local'}, $groupname;
	}
}

unless($error_msg)
{
	$gid = &find_gid_by_gname($groupname);
	if($gid < 0)
	{
		$error_msg = "";
		$newgid = &get_new_groupid();
		$entrylocalmembers = $in{"entrygmembers"};
		@line = split(/\s+/, $entrylocalmembers);

		$group{"pass"} = "";
		$group{"group"} = $groupname;
		$group{"gid"} = $newgid;
		$group{"members"} = join(",", @line);
	}
	else
	{
		if($groupname)
		{
			$error_msg = $text{'error_msg_group_exist_sys'};
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'groups_naslog_event_create_fail_gname_exist_sys'};
			$formatstr = sprintf $text{'groups_naslog_create_fail_gname_exist_sys'}, $groupname;	
		}
	}
}

if($error_msg)
{
#	&header($text{'index_title'}, "", undef, 0, 1);
#	print "<p>", $error_msg;
#	&footer("/groups/", $text{'index'});
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	&error_setup($text{"error_title_create_group"});
	&error($error_msg);
}
else
{
	&create_group(\%group);

	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_info'};
	$event = $text{'groups_naslog_event_create_success'};
	$formatstr = sprintf $text{'groups_naslog_create_success'}, $groupname;
	&nas_syslog($logminor, $logpriority, $event, $formatstr);

	&redirect("index.cgi");
}
